#include "myapp.h"
#include <QTextCodec>
#include <QList>
#include <QLibraryInfo>
#include <QLocale>
#include <QDebug>
#include "../settings/networksd.h"
#include "../settings/pluginssd.h"
#include "../settings/isettings.h"
#include "../core/core.h"

MyApp::MyApp(int &argc, char **argv, bool GUIenabled) :
	QApplication(argc, argv, GUIenabled),
	m_pTray(NULL)
{

	setApplicationName( "Beaver" );
	setOrganizationName( "Politechnika Łódzka" );
	setWindowIcon( QIcon(":/icon.svg") );

	//Ładujemy polskie/systemowe tłumaczenia komunikatów Qt
	m_qtTranslator.load("qt_" + QLocale::system().name(),
		  QLibraryInfo::location(QLibraryInfo::TranslationsPath));
	installTranslator(&m_qtTranslator);

	//ustawiamy kodowanie
	QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
	QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
	QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

	setQuitOnLastWindowClosed( false );

	if( GUIenabled ){
		m_pTray = new SystemTray( this );
		m_pTray->showIcon();
	}


	//initiate our Singleton
	if( Core::instance() == NULL )
		qFatal("cannot create CORE");

	Core::instance()->registerSettingPage( new NetworkSD() );
	Core::instance()->registerSettingPage( new PluginsSD() );
}

MyApp::~MyApp(){
	delete m_pTray;

	const QList<ISettings*>& list = Core::instance()->getSettingsPages();

	while( list.count() ){
		ISettings *tmp = list.at(0);
		Core::instance()->unregisterSettingPage( tmp );
		delete tmp;
	}

	Core::drop();
}
